package camera.view;

import android.annotation.TargetApi;
import android.graphics.SurfaceTexture;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.os.Build;
import android.util.Log;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import camera.CameraData;
import camera.CameraHolder;
import camera.CameraUtils;
import camera.OpenGl.CameraShaderProgram;
import camera.OpenGl.utils.GLUtils;
import camera.bean.CameraPic;
import camera.exception.CameraDisabledException;
import camera.exception.CameraHardwareException;
import camera.exception.CameraNotSupportException;
import camera.exception.NoCameraException;
import camera.mediarecorder.CameraRecorder;

/**
 *@author:ike
 *@function: x相机图像预览类
 *@creat time:14:57 2017/12/29
 **/

public class CameraRender implements GLSurfaceView.Renderer, SurfaceTexture.OnFrameAvailableListener {
    private String Tag="CameraRender";
    private GLSurfaceView mGlSurfaceView;
    private SurfaceTexture mSurfaceTexture;
    private CameraShaderProgram mProgram;
    private CameraPic cameraPic;

    public void setCameraRecorder(CameraRecorder cameraRecorder) {
        this.cameraRecorder = cameraRecorder;
    }

    private CameraRecorder cameraRecorder;
    private final float[] mTexMtx = GLUtils.createIdentityMtx();
    private final float[] mPosMtx=GLUtils.createIdentityMtx();
    /**
     * OpenGl实例对象Id
     */
    private int mSurfaceTextureId;
    /**
     * 相机打开状态
     */
    private boolean isCameraOpen;

    public CameraRender(RenderSurfaceView renderSurfaceView) {
        this.mGlSurfaceView=renderSurfaceView;
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        mProgram=new CameraShaderProgram(mGlSurfaceView.getContext());
        cameraPic=new CameraPic(mProgram);
        initSurfaceTexture();
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        //设置OpenGl预览窗口大小
       GLES20.glViewport(0,0,width,height);
        //开启相机预览
        startCameraPreview();
       
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        if (mSurfaceTexture!=null){
            mSurfaceTexture.updateTexImage();
            mSurfaceTexture.getTransformMatrix(mTexMtx);
        }
        draw();
        //开启视频编码录制
        if (cameraRecorder.isFirstSetUp()){
            if (cameraRecorder.isStarted()){
               // cameraRecorder.startRecod();
            }
            
        }
        if (cameraRecorder.isFirstSetUp()){
            
        }
        
    }

    /**
     * 绘制图像
     */
    private void draw(){
        cameraPic.draw(mPosMtx,mSurfaceTextureId);
    }

    /**
     * 初始化surfaceTexture
     */
    private void initSurfaceTexture() {
        int[] textures = new int[1];
        GLES20.glGenTextures(1, textures, 0);
        mSurfaceTextureId = textures[0];
        mSurfaceTexture = new SurfaceTexture(mSurfaceTextureId);
        mSurfaceTexture.setOnFrameAvailableListener(this);
        GLES20.glDisable(GLES20.GL_DEPTH_TEST);
        GLES20.glDisable(GLES20.GL_CULL_FACE);
        GLES20.glDisable(GLES20.GL_BLEND);
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mSurfaceTextureId);
        GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
                GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
                GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
                GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
                GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    }

    /**
     * 开启相机预览
     */
    private void startCameraPreview() {
        try {
            CameraUtils.checkCameraService(mGlSurfaceView.getContext());
        } catch (CameraDisabledException e) {
            Log.e(Tag,"相机不可用");
            e.printStackTrace();
            return;
        } catch (NoCameraException e) {
            Log.e(Tag,"没有相机，不可用");
            e.printStackTrace();
            return;
        }
        CameraHolder.State state = CameraHolder.instance().getState();
        CameraHolder.instance().setSurfaceTexture(mSurfaceTexture);
        if (state != CameraHolder.State.PREVIEW) {
            try {
                CameraHolder.instance().openCamera();
                CameraHolder.instance().startPreview();
                Log.e(Tag,"相机打开成功");
                isCameraOpen = true;
            } catch (CameraHardwareException e) {
                e.printStackTrace();
                Log.e(Tag,"相机打开失败");
            } catch (CameraNotSupportException e) {
                e.printStackTrace();
                Log.e(Tag,"相机打开相机不支持");
            }
        }
    }

    @Override
    public void onFrameAvailable(SurfaceTexture surfaceTexture) {
        mGlSurfaceView.requestRender();
        
    }
    
   
}
